package com.hushao.aimi.security;

import com.hushao.aimi.common.ResponseCode;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiUser;
import com.hushao.aimi.entity.Role;
import com.hushao.aimi.repository.RoleRepository;
import com.hushao.aimi.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 用户
 * @author: 胡少
 * @create: 2018-06-03 23:52
 **/
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AimiUser user=userRepository.findByUsername(username);
        if(user!=null){
            //获取权限
            List<Role> roles=roleRepository.listRoleByUser(user.getId());
            return new SecurityUserDetails(user,roles);
        }
        throw null;

    }
}
